package cn.pxs.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/api/info")
public class InfoController {//token版获取 user对象
    @GetMapping("/all")
    public Authentication getAuthentication(Authentication authentication){
        return authentication;
    }

    @ResponseBody
    @GetMapping("/getUser")
    //前端 通过 这里 去获取用户数据
    public Object getUser(){
        Object o =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("o = "+o);
        if(o == null ) return null;
        if(o instanceof UserDetails){
            return (UserDetails)o;
        }else {
            return String.valueOf(o);
        }

    }

}
